package com.wuming.caiwu.service.impl;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.WumingErrorCode;
import com.wuming.caiwu.dao.QsgHexiaoMapper;
import com.wuming.caiwu.model.QsgHexiao;
import com.wuming.caiwu.service.QsgHexiaoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QsgHexiaoServiceImpl extends BaseServiceImpl implements QsgHexiaoService {

    @Autowired
    private QsgHexiaoMapper qsgHexiaoMapper;

    @Override
    public List<QsgHexiao> getHexiaoOrderByPage(Long storeId, Integer hexiaoType, String startDate, String endDate, int pageIndex, int pageSize) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(null != storeId){
            params.put("storeId",storeId);
        }
        if(null != hexiaoType){
            params.put("hexiaoType",hexiaoType);
        }
        if(StringUtils.isNotBlank(startDate)){
            params.put("startDate",startDate + " 00:00:00");
        }
        if(StringUtils.isNotBlank(endDate)){
            params.put("endDate",endDate + " 23:59:59");
        }
        try{
            return (List<QsgHexiao>) this.getPageList(QsgHexiaoMapper.class, "selectByParams", params, pageIndex, pageSize);
        }catch(Exception e) {
            logger.error("查询订单信息失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(Long storeId, Integer hexiaoType, String startDate, String endDate) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(null != storeId){
            params.put("storeId",storeId);
        }
        if(null != hexiaoType){
            params.put("hexiaoType",hexiaoType);
        }
        if(StringUtils.isNotBlank(startDate)){
            params.put("startDate",startDate + " 00:00:00");
        }
        if(StringUtils.isNotBlank(endDate)){
            params.put("endDate",endDate + " 23:59:59");
        }
        try{
            return this.getTotal(QsgHexiaoMapper.class, "selectTotal", params);
        }catch(Exception e) {
            logger.error("查询订单总数失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }
}
